package com.fessc.develop.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fessc.develop.entity.comvo.BankCheckFileDownVo;
import com.fessc.develop.entity.dbvo.TCheckResult;
import com.fessc.develop.handler.BankCheckFileDownPushHandler;
import com.fessc.develop.model.enums.EnumPushType;
import com.fessc.develop.model.enums.EnumResultType;
import com.fessc.develop.model.enums.EnumTaskCode;
import com.fessc.develop.service.interfaces.ITCheckResultService;
import com.fessc.develop.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BankCheckFileDownJob extends ScheduleJob {

    @Resource
    private ITCheckResultService checkResultService;

    @Resource
    private BankCheckFileDownPushHandler bankCheckFileDownPushHandler;

    @Override
    public String execute() {
        log.info("对账文件下载");
        Date checkDate = null;
        try {
            checkDate = DateUtil.stringToDate(DateUtil.getLastDayDate(DateUtil.getCurrentDateTime(DateUtil.DATE_FORMAT), 1), DateUtil.DATE_FORMAT);
        } catch (Exception e) {
            log.info("获取前一天日期异常：" + e.getMessage());
        }
        // 查询对账申请成功且未下载的对账记录
        List<TCheckResult> bankCheckFileList = checkResultService.getBankCheckDownListByDate(getCCBBankId(), checkDate, EnumResultType.COM_TRAN_STATUS_SUCCESS.getStatusCode(), EnumResultType.COM_TRAN_STATUS_SUCCESS.getStatusCode());
        List<BankCheckFileDownVo> bankCheckFileDownVoList = new ArrayList<BankCheckFileDownVo>();
        bankCheckFileList.forEach(checkResult -> {
            BankCheckFileDownVo bankCheckFileDownVo = new BankCheckFileDownVo();
            BeanUtils.copyProperties(checkResult, bankCheckFileDownVo);
            bankCheckFileDownVo.setCheckDate(checkResult.getCheckDate());
            bankCheckFileDownVo.setBatchNo(checkResult.getBatchNo());
            bankCheckFileDownVoList.add(bankCheckFileDownVo);
        });
        bankCheckFileDownPushHandler.bankCheckFileDownPush(bankCheckFileDownVoList, EnumPushType.CHECKING_PUSH.toString());
        return null;
    }

    @Override
    public String getTaskCode() {
        return EnumTaskCode.BANK_CHECK_DOWN_PUSH.toString();
    }
}
